package csel.view;

import java.awt.event.*;
import java.util.LinkedList;
import java.util.List;

/**
 * This is an abstract class that provides some common functionality, such as
 * processing GameCommands? and implementing observer structure, for all viewports. 
 * @author David
 */
public abstract class Viewport
{
	List<ActionListener> actionListeners;
	List<KeyListener> keyListeners;
	
	public Viewport()
	{
		actionListeners = new LinkedList<ActionListener>();
		keyListeners = new LinkedList<KeyListener>();
	}
	
	/**
	 * Set whether the viewport is displayed or not.
	 */
	public abstract void setVisible(boolean visibility);
	
	/**
	 * Deconstruct the Viewport so that it may be garbage collected.
	 */
	public abstract void close();
	
	/**
	 * Process a GameCommand.
	 */
	public void accept(csel.model.GameCommand command)
	{
		command.execute();
	}
	
	public void addActionListener(ActionListener l)
	{
		actionListeners.add(l);
	}

	/**
	 * Remove the specified ActionListener from the internal collection.
	 * @return true if the ActionListener existed in the collection
	 */
	public boolean removeActionListener(ActionListener l)
	{
		return actionListeners.remove(l);
	}
	
	public void addKeyboardListener(KeyListener l)
	{
		keyListeners.add(l);
	}
	
	/**
	 * Remove the specified KeyListener from the internal collection.
	 * @return true if the KeyListener existed in the collection
	 */
	public boolean removeKeyListener(KeyListener l)
	{
		return keyListeners.remove(l);
	}
	
	public abstract boolean displayConfirm(String message);
	
	protected class TrampolineListener implements ActionListener, KeyListener
	{

		public void actionPerformed(ActionEvent event)
		{
			for (ActionListener l : actionListeners)
				l.actionPerformed(event);
		}

		public void keyPressed(KeyEvent event)
		{
			for (KeyListener l : keyListeners)
				l.keyPressed(event);
		}

		public void keyReleased(KeyEvent event)
		{
			for (KeyListener l : keyListeners)
				l.keyReleased(event);
		}

		public void keyTyped(KeyEvent event)
		{
			for (KeyListener l : keyListeners)
				l.keyTyped(event);
		}
		
	}
}
